
/**
 * @author: 王琛
 * @description: 生成UUID
 * @Date: 2021-11-12 17:07:37
 * @param {*}
 * @return {*}
 */
export function getUuid() {
    let s = [];
    let hexDigits = "0123456789abcdef";
    for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    let uuid = s.join("");
    return uuid;
}

/**
 * @description: 获取图片信息
 * @author: 王琛
 * @Date: 2022-08-15 16:06:50
 * @param {*}
 * @return {*}
 */
export function getImage(name) {
    const path = "/src/assets/images" + name;
    const modules = import.meta.globEager("/src/assets/images/**/*.{png,svg,jpg,jpeg}");
    if (modules[path]) return modules[path]["default"]
}

/**
 * @description: 吧对象转换为地址栏?号后面的参数
 * @author: 王琛
 * @Date: 2022-09-13 10:05:32
 * @param {*}
 * @return {*}
 */
export function objTransUrlParams(obj) {
    const params = []
    Object.keys(obj).forEach((key) => {
        let value = obj[key]
        if (typeof value === 'undefined') {
            value = ''
        }
        params.push([key, value].join('='))
    })
    return params.join('&')
}

/**
 * @description: json对象转换为form对象
 * @author: 王琛
 * @Date: 2022-09-13 14:33:25
 * @param {*}
 * @return {*}
 */
export function jsonTransformationForm(config) {
    let _form = new FormData();
    Object.keys(config).forEach((e, i) => {
        _form.append(e, config[e]);
    })
    return _form;
}

/**
 * @description: 时间推敲函数
 * @author: 王琛
 * @Date: 2022-09-19 16:37:29
 * @param {*}
 * @return {*}
 */
export function timeDeliberation({day = 7, format = "MM/DD"} = {}) {
    const _currentTime = dayjs();
    let format_over = [];
    for (let i = 0; i < day; i++) {
        format_over.unshift(_currentTime.subtract(i, "day").format(format))
    }
    return format_over;
}

/**
 * @description: 数据补位函数
 * @author: 王琛
 * @Date: 2022-09-26 10:24:48
 * @param {*}
 * @return {*}
 */
export function dataFilling({dateList = [], dataList = [], timeKey = "date", valKey = "num", defaultVal = 0} = {}) {
    return dateList.map(yLabelTime => {
        // 默认值
        let num = defaultVal || 0;
        // 根据当前的时间格式查找给的数据的字段来获取当前数据对象
        const dateItem = dataList.find(e => {
            return e[timeKey] == yLabelTime
        });
        // 更新要输入的值
        if (dateItem) num = parseInt(dateItem[valKey]);
        // 返出数据,或者补位值
        return num;
    })
}

/**
 * @description: 获取元素的宽高
 * @author: 王琛
 * @Date: 2022-11-24 10:16:57
 * @param {*}
 * @return {*}
 */
export function getElementWH(name) {
    let div = document.getElementById(name);
    if (!div) return {w: 0, h: 0};
    let w = div.offsetWidth;    // 返回元素的总宽度
    let h = div.offsetHeight;    // 返回元素的总高度
    return {w, h}
}